﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;
using Iudicium.Servicio;
using Iudicium.Servicio.CRN;
using System.Web.Script.Serialization;

public partial class UserControls_SelectResponsibles : System.Web.UI.UserControl
{
    private static readonly ILog log = LogManager.GetLogger("LogFile");

    #region Propiedades

    public int ServicioId
    {
        set { ServicioIdHiddenField.Value = value.ToString(); }
        get
        {
            int servicioId = 0;
            try
            {
                servicioId = Convert.ToInt32(ServicioIdHiddenField.Value);
            }
            catch (Exception ex)
            {
                log.Error("No se pudo convertir ServicioIdHiddenField.Value a un valor entero", ex);
            }
            return servicioId;
        }
    }

    public bool ReadOnly
    {
        set { ReadOnlyHiddenField.Value = value.ToString(); }
        get
        {
            bool readOnly = false;
            try
            {
                readOnly = Convert.ToBoolean(ReadOnlyHiddenField.Value);
            }
            catch (Exception ex)
            {
                log.Error("No se pudo convertir ReadOnlyHiddenField.Value a un valor boolean", ex);
            }
            return readOnly;
        }
    }

    public string SearchRequestHandler
    {
        get { return VirtualPathUtility.ToAbsolute("~/UserControls/Handlers/SearchAdministrativeHandler.ashx"); }
    }

    public List<ResponsableJSON> NuevosResponsables
    {
        get
        {
            List<ResponsableJSON> list = null;
            try
            {
                JavaScriptSerializer js = new JavaScriptSerializer();
                list = js.Deserialize<List<ResponsableJSON>>(NuevosResponsablesHiddenField.Value);
            }
            catch (Exception ex)
            {
                log.Error("No se pudo transaformar NuevosResponsablesHiddenField.Value a List<ResponsableJSON>", ex);
                list = new List<ResponsableJSON>();
            }
            return list;
        }
    }

    public List<ResponsableJSON> ResponsablesParaHistorial
    {
        get
        {
            List<ResponsableJSON> list = null;
            try
            {
                JavaScriptSerializer js = new JavaScriptSerializer();
                list = js.Deserialize<List<ResponsableJSON>>(ResponsablesInactivosHiddenField.Value);
            }
            catch (Exception ex)
            {
                log.Error("No se pudo transaformar ResponsablesInactivosHiddenField.Value a List<ResponsableJSON>", ex);
                list = new List<ResponsableJSON>();
            }
            return list;
        }
    }

    public List<int> ResponsablesEliminados
    {
        get
        {
            List<int> list = null;
            try
            {
                JavaScriptSerializer js = new JavaScriptSerializer();
                list = js.Deserialize<List<int>>(ResponsablesEliminadosHiddenField.Value);
            }
            catch (Exception ex)
            {
                log.Error("No se pudo transaformar ResponsablesInactivosHiddenField.Value a List<int>", ex);
                list = new List<int>();
            }
            return list;
        }
    }

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        Update();
    }

    public void Update()
    {
        List<Responsable> list = new List<Responsable>();
        if (ServicioId > 0)
        {
            try
            {
                list = ResponsableCRN.GetResponsablesActivosByServicioId(ServicioId);
            }
            catch (Exception ex)
            {
                log.Error("Ocurrio un error al obtener los responsables del servicio " + ServicioId, ex);
            }
        }
        if (ReadOnly)
        {
            OpenDialog.Visible = false;
        }
        ActiveResponsibleRepeater.DataSource = list;
        ActiveResponsibleRepeater.DataBind();
    }

    public string GetControlValue()
    {
        return ActiveResponsibleRepeater.Items.Count == 0 ? "" : "1";
    }

    public string GetValueHtml()
    {
        return "<input type='hidden' value='' name='" + UniqueID + "' id='" + ClientID + "'/>";
    }

    protected string GetClassName(string tipo)
    {
        return ClientID + "_" + tipo;
    }

    protected bool IsRepeaterVisible()
    {
        return ActiveResponsibleRepeater.Items.Count == 0;
    }
}